//
//  GoodsEvaluationCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/20.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GoodsEvaluationCell.h"
#import "GoodsCommentModel.h"
#import "GoodsCommentCell.h"
#import "GoodCommentsViewController.h"

@interface GoodsEvaluationCell()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic,strong) UIView *leftFlagView;

@property (nonatomic,strong) UILabel *totalEvaluationLB;//全部评价lb

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) UIView *lineV;

@property (nonatomic,strong) NSMutableArray *dataSourceArr;

@end

@implementation GoodsEvaluationCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.dataSourceArr = [NSMutableArray array];
        [self initUI];
    }
    return self;
}
#pragma mark - setter
- (void)setModel:(GoodsDetailModel *)model{
    _model = model;
    self.totalEvaluationLB.text = [NSString stringWithFormat:@"商品评价 (%ld)",(long)_model.comments.count];
    self.dataSourceArr = [NSMutableArray arrayWithArray:_model.comments];
    [self.tableView reloadData];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.tableView.height = self.tableView.contentSize.height-16;
        self.lineV.top = self.tableView.bottom;
        [self.contentView addSubview:self.lineV];
        
        if (self.updateCellHeightBlock) {
            self.updateCellHeightBlock(self.lineV.bottom);
        }
    });
}
- (void)setAllEvaluationArr:(NSArray *)allEvaluationArr{
    _allEvaluationArr = allEvaluationArr;
    self.dataSourceArr = [NSMutableArray arrayWithArray:_allEvaluationArr];
    [self.tableView reloadData];
}
#pragma mark - UI
-(void)initUI{
    [self.contentView addSubview:self.leftFlagView];
    [self.contentView addSubview:self.totalEvaluationLB];
    [self.contentView addSubview:self.tableView];
    
    UILabel *detailLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 144, self.totalEvaluationLB.top+1, 100, 18)];
    detailLB.textColor = MainPureColor(153);
    detailLB.font = FontSet(13);
    detailLB.text = @"查看全部";
    detailLB.textAlignment = NSTextAlignmentRight;
    detailLB.userInteractionEnabled = YES;
    [self.contentView addSubview:detailLB];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    tap.delegate = self;
    [detailLB addGestureRecognizer:tap];
    
    UIImageView *moreImgV = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 30, detailLB.top+3, 6, 12)];
    moreImgV.image = LoadImage(@"个人中心_查看全部");
    moreImgV.userInteractionEnabled = YES;
    [self.contentView addSubview:moreImgV];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    tap1.delegate = self;
    [moreImgV addGestureRecognizer:tap1];
    
    self.tableView.height = self.tableView.contentSize.height;
    self.lineV.top = self.tableView.bottom;
    [self.contentView addSubview:self.lineV];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.updateCellHeightBlock) {
            self.updateCellHeightBlock(self.lineV.bottom+15);
        }
    });
}
#pragma mark - lazy
- (UIView *)leftFlagView{
    if (!_leftFlagView) {
        _leftFlagView = [[UIView alloc] initWithFrame:CGRectMake(15, 24, 4, 14)];
        _leftFlagView.backgroundColor = RGB(26, 128, 255);
        _leftFlagView.clipsToBounds = YES;
        _leftFlagView.layer.cornerRadius = 2;
    }
    return _leftFlagView;
}
- (UILabel *)totalEvaluationLB{
    if (!_totalEvaluationLB) {
        _totalEvaluationLB = [[UILabel alloc] initWithFrame:CGRectMake(self.leftFlagView.right+8, self.leftFlagView.top-3, ScreenWidth/2+20, 20)];
        _totalEvaluationLB.textColor = MainBlackTextColor;
        _totalEvaluationLB.font = FontSet(14);
        _totalEvaluationLB.text = @"商品评价 （1000+）";
    }
    return _totalEvaluationLB;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.totalEvaluationLB.bottom+15, ScreenWidth, 100) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = UIColor.whiteColor;
        [_tableView setTableHeaderView:[self tableHeaderAndFooterView]];
        [_tableView setTableFooterView:[self tableHeaderAndFooterView]];
    }
    return _tableView;
}
-(UIView *)tableHeaderAndFooterView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1f)];
    return view;
}
- (UIView *)lineV{
    if (!_lineV) {
        _lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 12)];
        _lineV.backgroundColor = MainBackColor;
    }
    return _lineV;
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodsCommentModel *model = self.dataSourceArr[indexPath.row];
    return model.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{return 0.1f;}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{return 0.1f;}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodsCommentModel *model = self.dataSourceArr[indexPath.row];
    GoodsCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsCommentCell"];
    if (!cell) {
        cell = [[GoodsCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodsCommentCell"];
    }
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark - SEL
//查看全部评价
-(void)tapClick:(UIGestureRecognizer *)tap{
    GoodCommentsViewController *vc = [[GoodCommentsViewController alloc] init];
    vc.productId = self.model.ID;
    [[Configuration getCurrentVC].navigationController pushViewController:vc animated:YES];
}

@end


